/**
 * Created by aboc on 17-3-23.
 */

$.fn.eslider = function(options){
    /**
     *  value   当前滑块的值
     *  cslider 中间的小滑块
     *  lslider 左边的小滑块
     *  rslider 右边的小滑块
     *  bbox  下面的盒子
     *  tbox  上面的绿色条条
     *  min  最小值
     *  max  最大值
     *  lmin 限制最小值
     *  lmax 限制最大值
     *  drag 是否可以拖动
     *  bboxwidth: 盒子的宽度，自动获取
     *
     */
    var defaults = {
        debug:false,
        value:0,
        cslider:".cslider",
        lslider:".lslider",
        rslider:".rslider",
        bbox:".bbox",
        tbox:".tbox",
        min:0,
        max:100,
        lmin:0,
        lmax:100,
        drag:true,
        bboxwidth:0,
        type:"int",
        callback:null
    };
    this.drag_clientX = 0;
    this.drag_obj = "";
    this._options = $.extend(defaults,options);
    this.dom = $(this);
    this._options.bboxwidth = $(this._options.bbox,this.dom).width();

    this.dom.css("position","relative");
    $(this._options.cslider+","+this._options.lslider+","+this._options.rslider+","+this._options.tbox,this.dom).css("position","absolute");
    $("img",this.dom).attr("draggable","false");

    this.value_width = parseFloat(this._options.bboxwidth/ (this._options.max - this._options.min) );
    this.cslider_width = $(this._options.cslider, this.dom).width();
    this.lslider_width = $(this._options.lslider, this.dom).width();
    this.rslider_width = $(this._options.rslider, this.dom).width();
    var o = this;
    this.set_cslider = function(value){
        o._options.value = parseFloat(o._options.value);
        o._options.lmin = parseFloat(o._options.lmin);
        o._options.lmax = parseFloat(o._options.lmax);
        if(typeof value != "undefined"){
            if(o._options.debug)console.trace( o._options.value)
            if(value <= o._options.lmin){
                o._options.value = o._options.lmin;
            }
            else if(value >= o._options.lmax){
                o._options.value = o._options.lmax;
            } else {
                o._options.value = value;
            }
        }
        //console.trace();
        //console.info(o._options.value)
        //console.info(o._options.value* o.value_width)
        $(o._options.cslider, o.dom).css("left",(o._options.value- o._options.min)* o.value_width - o.cslider_width/2);
    }
    this.set_lslider = function(value){
        o._options.value = parseFloat(o._options.value);
        o._options.lmin = parseFloat(o._options.lmin);
        o._options.lmax = parseFloat(o._options.lmax);
        o._options.min = parseFloat(o._options.min);
        o._options.max = parseFloat(o._options.max);
        if(typeof value != "undefined"){
            if(value <= o._options.min){
                o._options.lmin = o._options.min;
            }
            else if(value >= o._options.max){
                o._options.lmin = o._options.max;
            }
            else if (value >= o._options.lmax){
                o._options.lmin = o._options.lmax;
            }
            else {
                o._options.lmin = value;
            }
        }
        if(o._options.lmin > o._options.value){
            console.info(typeof o._options.lmin,o._options.lmin , typeof  o._options.value, o._options.value,o._options.lmin > o._options.value);
            o._options.value = o._options.lmin;
            o.set_cslider();
        }
        $(o._options.lslider, o.dom).css("left",(o._options.lmin- o._options.min)* o.value_width- o.lslider_width);
    }
    this.set_rslider = function(value){
        o._options.value = parseFloat(o._options.value);
        o._options.lmin = parseFloat(o._options.lmin);
        o._options.lmax = parseFloat(o._options.lmax);
        o._options.min = parseFloat(o._options.min);
        o._options.max = parseFloat(o._options.max);
        if(typeof value != "undefined"){
            if(value > o._options.max){
                o._options.lmax = o._options.max;
            }
            else if(value < o._options.min){
                o._options.lmax = o._options.min;
            }
            else if (value < o._options.lmin){
                o._options.lmax = o._options.lmin;
            }
            else {
                o._options.lmax = value;
            }
        }
        if(o._options.lmax < o._options.value){
            o._options.value = o._options.lmax;
            o.set_cslider();
        }
        $(o._options.rslider, o.dom).css("left",(o._options.lmax - o._options.min)* o.value_width);
    }
    this.set_tbox = function(){
        var value = o._options.lmax - o._options.lmin;
        $(o._options.tbox, o.dom).css({
            width:value* o.value_width,
            marginLeft:(o._options.lmin- o._options.min) * o.value_width
        });
    }


    $(o.dom).delegate(o._options.cslider,"mousedown",function(d){
                o.drag_obj = "cslider";
                o.drag_clientX = d.clientX;
        d.preventDefault();
    });
    $(o.dom).delegate(o._options.lslider,"mousedown",function(d){
        o.drag_obj = "lslider";
        o.drag_clientX = d.clientX;
        d.preventDefault();
    });
    $(o.dom).delegate(o._options.rslider,"mousedown",function(d){
        o.drag_obj = "rslider";
        o.drag_clientX = d.clientX;
        d.preventDefault();
    });
    $(o.dom).delegate("img","mousedown",function(d){
        d.preventDefault();
    });

    $(o.dom).mousemove(function(d){
        if(o.drag_obj ==""){
            return;
        }
        if( o.value_width <=0){
            return;
        }
        var offset = d.clientX - o.drag_clientX;
        o.drag_clientX = d.clientX;
        o._options.value = parseFloat(o._options.value);
        switch (o.drag_obj){
            case "cslider":
                o._options.value += parseFloat(offset / o.value_width);
                //console.info(offset, o._options.value,"cslider")
                o.set_cslider(o._options.value);
                o.set_lslider();
                o.set_rslider();
                break;
            case "lslider":
                o._options.lmin += parseFloat(offset / o.value_width);
                o.set_cslider();
                o.set_lslider(o._options.lmin);
                o.set_rslider();
                break;
            case "rslider":
                o._options.lmax += parseFloat(offset / o.value_width);
                o.set_cslider();
                o.set_lslider();
                o.set_rslider(o._options.lmax);
                break;
        }
        o.set_tbox();
        o.callback();
        d.preventDefault();
    });

    $("body").mouseup(function(){
        if(o.drag_obj ==""){
            return;
        }
        o.drag_obj = "";
        o.callback();
    });
    $(o.dom).mouseleave(function(){
        if(o.drag_obj ==""){
            return;
        }
        o.drag_obj = "";
        o.callback();
    });
    /**
     * 格式化数字
     * @param value
     * @returns {*}
     * @private
     */
    this._format = function(value){
        switch (o._options.type){
            case "int":
                value = parseInt(value);
                break;
            case "float":
                value = parseFloat(value.toFixed(2));
                break;
        }
        return value;
    }
    /**
     * 回调
     */
    this.callback = function(){
        if(o._options.callback != null && typeof o._options.callback == "function"){
            o._options.callback.call({},{
                value:o._format(o._options.value),
                lmax:o._format(o._options.lmax),
                lmin:o._format(o._options.lmin),
                max:o._format(o._options.max),
                min:o._format(o._options.min)
            });
        }
    }
    this.init = function(){
        o._options.bboxwidth = $(o._options.bbox,o.dom).width();
        o.value_width = parseFloat(o._options.bboxwidth/ (o._options.max - o._options.min)) ;
        o.cslider_width = $(o._options.cslider, o.dom).width();
        o.lslider_width = $(o._options.lslider, o.dom).width();
        o.rslider_width = $(o._options.rslider, o.dom).width();
        o.set_cslider();
        o.set_lslider();
        o.set_rslider();
        o.set_tbox();
        o.callback();
    }

    this.init();
    return this;
}
